import type { FolderData, MailDataAny, MailEnvelope, MailStructure } from 'coremail/src/main/ets/api';
import { MailAttribute } from 'coremail/src/main/ets/api';
import { CmTimeManager } from '../cm-time-manager/CmTimeManager';
import { FolderEntity } from '../entities/FolderEntity';
import { MailEntity } from '../entities/MailEntity';


/**
 * 负责进行类型转换
 * MailEntity <--> MailDataAny
 * FolderEntity <--> FolderData
 */
export class EntityTransformer {

  /**
   * FolderEntity ==> FolderData
   * TODO: type之间是否是通用的?
   * @param entity
   * @returns
   */
  static getFolderDataWithFullName(entity:FolderEntity,fullName:string):FolderData {
    let parent = "";
    const i = fullName.lastIndexOf('/');
    if (i > 0) {
      parent = fullName.substring(0, i);
    }
    let folderData:FolderData = {
      name:entity.name,
      fullName:fullName,
      type:entity.type,
      parent: parent,
      unreadCount:entity.unReadCount,
      mailCount: entity.mailCount
    }
    return folderData;
  }
  static getFolderDataWithParentFullName(entity:FolderEntity,parentFullName:string):FolderData {
    let folderData:FolderData = {
      name:entity.name,
      fullName:`${parentFullName}/${entity.name}`,
      type:entity.type,
      parent:parentFullName,
      unreadCount:entity.unReadCount
    }
    return folderData;
  }

  static getFolderEntity(folderData:FolderData,parentId:number|null = null,order:number|null = null) : FolderEntity {
    let entity = new FolderEntity();
    entity.name = folderData.name;
    entity.parentId = null;
    entity.type = folderData.type;
    entity.unReadCount = folderData.unreadCount;
    entity.mailCount = folderData.mailCount;
    entity.orderNum = order;
    return entity;
  }


  /**
   * MailEntity ==> MmailDataAny
   * @param data
   * @returns
   */
  static getMailDataAny(entity: MailEntity): MailDataAny {
    let attr = entity.getAttr();
    let size = entity.size;
    let envelop:MailEnvelope = entity.getEnvelope();
    let structure:MailStructure = JSON.parse(entity.structure) as MailStructure;
    let mailDataAny: MailDataAny = {
      id: entity.mid,
      envelope: envelop,
      structure: structure,
      attributes: attr,
      size: size
    }
    return mailDataAny;
  }

  /**
   * 不做解析,将mail源文保存到本地,然后存储到数据库.
   *
   * @param mail
   * @param mid
   * @param folderFullName
   * @returns
   */
  static getMailEntityWithMimeString(mail:string,mid:string,folderFullName:string):MailEntity {
    let entity = new MailEntity();
    entity.mid = mid;
    // entity.saveMail(mail,)
    return entity;
  }

  /**
   * MailDataAny ==> MailEntity
   * @param mailData
   * @param folderName
   * @returns
   */
  static getMailEntity(mailData: MailDataAny,folderId:number):MailEntity {
    let entity = new MailEntity();
    entity.folderId = folderId;
    entity.mid = mailData.id;
    entity.size = mailData.size;
    let envelope: MailEnvelope = mailData.envelope!;
    entity.setEnvelope(envelope);
    let receiveDate = envelope.date;
    entity.receivedDate = receiveDate.getTime()
    entity.subject = envelope.subject;
    entity.sender = JSON.stringify(envelope.from);
    entity.receiver = JSON.stringify(envelope.to);
    entity.cc = JSON.stringify(envelope.cc);
    entity.bcc = JSON.stringify(envelope.bcc);
    let structure: MailStructure = mailData.structure!
    entity.structure = JSON.stringify(structure);

    //解析 new flag 属性
    entity.isNew = true;
    entity.isFlag = false;
    if(mailData.attributes.length > 0) {
      mailData.attributes.forEach((attr:MailAttribute)=>{
        switch(attr){
          case MailAttribute.Seen:{
            entity.isNew = false;
          }
          case MailAttribute.Flagged:{
            entity.isFlag = true;
          }
        }
      })
    }
    return entity;
  }


}